package org.niux.AutoLoginQQ.operate;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.niux.AutoLoginQQ.common.Constant;

/**
 * 
 * Copyright (c) 2012,niux<br>
 * All rights reserved.<br>
 * 
 * 文件名称：CheckVersion.java<br>
 * 摘 要：版本检测<br>
 * <br>
 * 当前版本：<br>
 * 作 者：mingxian Email: <a
 * href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：@since 2012-9-26<br>
 * <br>
 * 取代版本：<br>
 * 原作者 ：<br>
 * 完成日期：@since 2012-9-26<br>
 */
public class CheckVersion {
	private Log log = LogFactory.getLog(this.getClass());

	/**
	 * 
	 * 方法概述：获取版本号列表数据
	 * 描述：获取版本号列表数据
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2012-9-26
	 */
	public String getVersion(String uuid){
		HttpClient httpclient = new DefaultHttpClient();

		// 创建httpget.
		HttpGet httpget = new HttpGet("http://niux.kuphp.net/check.php?uuid=" + uuid);
		
		try {
			HttpResponse response = httpclient.execute(httpget);
			
			// 获取响应实
			HttpEntity entity = response.getEntity();

			String responseString = EntityUtils.toString(entity);

			log.debug(responseString);
			
			String[] jsonString = responseString.split("<script");
			
			log.debug(jsonString[0]);
			
			httpget.releaseConnection();
			httpget.abort();
			
			return jsonString[0];
		} catch (IOException e) {
			log.debug("网络连接超时");
			e.printStackTrace();
			
			httpget.releaseConnection();
			httpget.abort();
			return null;
		}
	}
	
	/**
	 * 
	 * 方法概述：检查版本可用性
	 * 描述：检查版本可用性
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2012-9-26
	 */
	public String checkVersion(String uuid){
		String responseJson = getVersion(uuid);
		
		if("".equals(responseJson) || responseJson == null){
			return "false";
		}
		
		JSONArray versoinListJson = new JSONArray();
		
		String rs = "";
		
		try{
			versoinListJson = JSONArray.fromObject(responseJson.toString()); // 由JSON转成JSONOBJECT对象
		} catch (Exception e) {
			log.error(e);
			return "false";
		}
		
		log.debug(versoinListJson);
//		String status = versoinJson.getString("status");
		
		for (Object object : versoinListJson) {
			JSONObject versionObj = JSONObject.fromObject(object);
			String version = versionObj.getString("version");
			
			if(Constant.VERSION.contains(version)){
				String status = versionObj.getString("status");
				if(status.equals("0")){
					rs = "";
				} else if(status.equals("-1")){
					responseJson = getNewVersion();
					
					JSONObject newVersionJson = JSONObject.fromObject(responseJson); // 由JSON转成JSONOBJECT对象
					String newVersion = newVersionJson.getString("version");
					String path = newVersionJson.getString("path");
					
					rs = "false|" + newVersion + "|" + path;
				} else {
					responseJson = getNewVersion();
					
					log.debug(responseJson);
					JSONObject newVersionJson = JSONObject.fromObject(responseJson); // 由JSON转成JSONOBJECT对象
					String newVersion = newVersionJson.getString("version");
					String path = newVersionJson.getString("path");
					
					rs = "warn|" + newVersion + "|" + path;
				}
			}
		}
		
		return rs;
	}
	
	public static void main(String[] args) {
		String t = "[﻿﻿﻿﻿﻿{\"id\":\"1\",\"version\":\"2.5.1\",\"status\":\"-1\"}," +
				"{\"id\":\"2\",\"version\":\"2.1.0\",\"status\":\"-1\"},{\"id\":" +
				"\"3\",\"version\":\"2.1.1\",\"status\":\"-1\"},{\"id\":\"4\"," +
				"\"version\":\"2.1.2\",\"status\":\"-1\"},{\"id\":\"5\",\"version\"" +
				":\"2.1.3\",\"status\":\"-1\"},{\"id\":\"6\",\"version\":\"2.1.4\"" +
				",\"status\":\"-1\"},{\"id\":\"7\",\"version\":\"2.1.5\",\"status\"" +
				":\"-1\"},{\"id\":\"8\",\"version\":\"2.2.0\",\"status\":\"-1\"},{\"id\"" +
				":\"9\",\"version\":\"2.2.1\",\"status\":\"-1\"},{\"id\":\"10\",\"version\"" +
				":\"2.3.0\",\"status\":\"-1\"},{\"id\":\"11\",\"version\":\"2.3.1\"," +
				"\"status\":\"-1\"},{\"id\":\"12\",\"version\":\"2.4.0\",\"status\":\"-1\"}" +
				",{\"id\":\"13\",\"version\":\"2.5.0\",\"status\":\"-1\"},{\"id\":\"14\"," +
				"\"version\":\"2.5.2\",\"status\":\"-1\"},{\"id\":\"15\",\"version\":\"2.6.0\"" +
				",\"status\":\"-1\"},{\"id\":\"16\",\"version\":\"2.6.1\",\"status\":\"-1\"}," +
				"{\"id\":\"17\",\"version\":\"2.6.2\",\"status\":\"-1\"},{\"id\":\"18\"," +
				"\"version\":\"2.6.3\",\"status\":\"-1\"},{\"id\":\"19\",\"version\":\"2.6.4\"," +
				"\"status\":\"-1\"},{\"id\":\"20\",\"version\":\"2.6.5\",\"status\":\"-1\"}," +
				"{\"id\":\"21\",\"version\":\"2.7.0\",\"status\":\"-1\"},{\"id\":\"22\"," +
				"\"version\":\"2.7.1\",\"status\":\"-1\"},{\"id\":\"23\",\"version\":\"2.8.0\"," +
				"\"status\":\"-1\"},{\"id\":\"24\",\"version\":\"2.9.0\",\"status\":\"-1\"}," +
				"{\"id\":\"25\",\"version\":\"2.9.1\",\"status\":\"-1\"},{\"id\":\"26\"," +
				"\"version\":\"2.10.0\",\"status\":\"0\"}]";
		try {
			System.out.println(JSONArray.fromObject(t.getBytes("utf8")));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * 方法概述：获取最新版本号及下载地址
	 * 描述：获取最新版本号及下载地址
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2012-9-26
	 */
	public String getNewVersion(){
		HttpClient httpclient = new DefaultHttpClient();

		// 创建httpget.
		HttpGet httpget = new HttpGet("http://niux.kuphp.net/GetNewVersion.php");
		
		try {
			HttpResponse response = httpclient.execute(httpget);

			// 获取响应实
			HttpEntity entity = response.getEntity();

			String responseString = EntityUtils.toString(entity);
			
			String[] jsonString = responseString.split("<script");
			
			log.debug(jsonString[0]);
			
			httpget.releaseConnection();
			httpget.abort();
			
			return jsonString[0];
		} catch (IOException e) {
			log.debug("网络连接超时");
			e.printStackTrace();
			httpget.releaseConnection();
			httpget.abort();
			return null;
		}
	}
}
